import wx from './wx'
import constants from '../constants/constants'

export default {
  showShortToast(content) {
    return new Promise((resolve, reject) => {
      wx.showToast({ title: content, icon: 'none', image: null, duration: 1000})
    })
  },
  showLongToast(content) {
    return new Promise((resolve, reject) => {
      wx.showToast({title: content, icon: null, image: null, duration: 2000})
    })
  },
  jumper(path) {
    return new Promise((resolve, reject) => {
      wx.navigateTo({url: path})
    })
  },
  isLogin() {
    const token = wx.getStorageSync(constants.STORE_CONSTANTS.KEY_TOKEN)
    return !this.isEmpty(token)
  },
  getToken() {
    const token = wx.getStorageSync(constants.STORE_CONSTANTS.KEY_TOKEN)
    return this.isEmpty(token) ? "" : token
  },
  getUserInfo() {
    const userInfo = wx.getStorageSync(constants.STORE_CONSTANTS.KEY_USERINFO)
    return this.isNull(userInfo) ? null : userInfo
  },
  getUid() {
    const userInfo = wx.getStorageSync(constants.STORE_CONSTANTS.KEY_USERINFO)
    return this.isNull(userInfo) ? userInfo.uid : ''
  },
  isNull(obj) {
    return (obj == null || typeof (obj) == 'undefined' || typeof (obj) == 'null' || obj == 'null')
  },
  nullValueCheck (arr) {
    if (arr && arr.length > 0) {
      var item, value, tip;
      for (var i in arr) {
        item = arr[i];
        value = item[0];
        tip = item[1];
        if (!value || "" == value || null == value) {
          wx.showToast({ title: tip, icon: 'none', image: null, duration: 1000 })
          return false;
        }
      }
    }
    return true;
  },
  isArrayEmpty(obj) {
    if (this.isNull(obj)) {
      return true
    }
    if (Array.prototype.isPrototypeOf(obj) && obj.length == 0) {
      return true
    }
    if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length == 0) {
      return true
    }
    return false
  },
  isEmpty(str) {
    return (str == null || str == undefined || str == '')
  },
  back(delta) {
    wx.navigateBack({delta: delta})
  },
  setTitleBarTitle(content){
    wx.setNavigationBarTitle({ title: content})
  }
}


